package com.zlz.mapreduce.join;

import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: zlz
 * @date: 2022/1/3 13:17
 * @description:
 * @since: JDK1.8
 */
public class JoinMapper extends Mapper<LongWritable, Text, Text, NullWritable> {
    private Map<String, String> cacheMap = new HashMap<>();
    private Text outK = new Text();
    @Override
    protected void setup(Context context) throws IOException, InterruptedException {
        // 获取缓存文件
        URI[] cacheFiles = context.getCacheFiles();
        FileSystem fs = FileSystem.get(context.getConfiguration());
        FSDataInputStream fis = fs.open(new Path(cacheFiles[0]));

        // 从流中读取数据
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fis, StandardCharsets.UTF_8));
        String line;
        while (StringUtils.isNoneEmpty(line = bufferedReader.readLine())) {
            // 格式化 将\t替换为空格
            String replace = line.replace("\t", StringUtils.SPACE);

            // 切割
            String[] split = StringUtils.split(replace, StringUtils.SPACE);
            cacheMap.put(split[0], split[1]);
        }
        IOUtils.closeStreams(fis, bufferedReader);
    }

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line = value.toString();
        // 格式化 将\t替换为空格
        String replace = line.replace("\t", StringUtils.SPACE);

        // 切割
        String[] split = StringUtils.split(replace, StringUtils.SPACE);
        String pName = cacheMap.get(split[1]);
        String join = StringUtils.join(split[0], "\t", pName, "\t", split[2]);
        outK.set(join);
        context.write(outK, NullWritable.get());
    }
}
